package com.crazy.common.form;

import static com.crazy.common.form.Token.TOKEN;
import static java.lang.String.format;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * 生成令牌标签类
 * 
 * @author zhang weiwei
 */
@SuppressWarnings("serial")
public class TokenTag extends TagSupport {
	@Override
	public int doEndTag() throws JspException {
		JspWriter out = this.pageContext.getOut();
		HttpSession session = this.pageContext.getSession();
		try {
			String format = "<input type=\"hidden\" id=\"%s\" name=\"%s\" value=\"%s\"/>";
			Object sessionToken = session.getAttribute(TOKEN);
			out.println(format(format, TOKEN, TOKEN, sessionToken));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}
}
